/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sunspotworld;

import com.sun.spot.io.j2me.radiogram.Radiogram;
import com.sun.spot.io.j2me.radiogram.RadiogramConnection;
import com.sun.spot.sensorboard.peripheral.ISwitch;
import com.sun.spot.sensorboard.peripheral.ISwitchListener;
import com.sun.spot.util.Utils;
import java.io.IOException;
import org.sunspotworld.StartApplication.*;

/**
 *
 * @author Tomas Henriquez, Sabin Larrañaga
 */
public class ISwitchListenerImplSw2 implements ISwitchListener {

    private StartApplication sa;
    private RadiogramConnection lazerConn;

    public ISwitchListenerImplSw2(StartApplication sa, RadiogramConnection conn) {
        this.sa = sa;
        lazerConn = conn;
    }

    /**
     * //here is where we manage the flag values. This procedure will be launch when the left switch is pressed
     * @param sw
     */
    public void switchPressed(ISwitch sw) {
        //RadiogramConnection conn = null;
        System.out.println("Lazer!!");
        try {
            //conn = (RadiogramConnection) Connector.open("radiogram://"+lazerAddr+":" +sa.RUN_PORT);
            Radiogram dg = (Radiogram)lazerConn.newDatagram(lazerConn.getMaximumLength());
            dg.reset();
            dg.writeInt(1); //here we send the Lazor code
            lazerConn.send(dg);

            Utils.sleep(6000);


        } catch (IOException ex) {
            ex.printStackTrace();
        } 
    }

    public void switchReleased(ISwitch sw) {
    }
}
